package com.admin.exam.service;

import com.admin.exam.entity.Question;
import com.admin.exam.entity.QuestionImport;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSONArray;

import java.util.ArrayList;
import java.util.List;

public class ExcelListener extends AnalysisEventListener<QuestionImport> {
    private QuestionService questionService;

    public ExcelListener(QuestionService questionService) {
        super();
        this.questionService = questionService;
    }

    private List<Question> list = new ArrayList<>();

    @Override
    public void invoke(QuestionImport data, AnalysisContext analysisContext) {
        JSONArray answers = data.getAnswers();
        String content = data.getContent();
        Question question = new Question();
        question.setAnswer(answers.toJSONString());
        question.setContent(content);
        question.setScore("4");
        question.setSingle("true");
        list.add(question);
    }


    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        for (Question question : list) {
            questionService.add(question);
        }
    }
}